package com.site.listener;

import javax.servlet.*;
import java.util.HashMap;
import java.util.Map;

/*上下文监听器*/
public class ApplicationListener implements ServletContextListener, ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        System.out.println("application上下文添加了add属性："+scae.getName()+"="+scae.getValue());
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        System.out.println("application上下文remove属性："+scae.getName());
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        System.out.println("application上下文replace属性："+
                scae.getName()+
                ",old:"+ scae.getValue() +
                ",new:"+ scae.getServletContext().getAttribute(scae.getName()));
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("application上下文初始化完成");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("application上下文销毁完成");
        ServletContext servletContext = sce.getServletContext();
        try {
            //模拟数据库脏文
            Map<String ,String> map = new HashMap<>();
            map.put("你我","***");
            map.put("测试","***");
            map.put("<","&lt;");
            map.put(">","&gt;");
            map.put("'","&quat");
            servletContext.setAttribute("textMap",map);
            System.out.println("脏文加载完毕");
        }catch (Exception e){

        }

    }
}
